<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>后台管理系统</title>
        <meta name="author" content="BaiYY"/>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
        <meta name="renderer" content="webkit">
        <link rel="stylesheet" type="text/css" href="__PUBLIC__/admin/css/bootstrap.min.css"/>
        <link rel="stylesheet" type="text/css" href="__PUBLIC__/admin/css/Font/css/font-awesome.min.css"/>
        <link rel="stylesheet" type="text/css" href="__PUBLIC__/admin/css/index.css"/>
        <link rel="stylesheet" type="text/css" href="__PUBLIC__/admin/css/pages.css"/>
        <script type="text/javascript" src="__PUBLIC__/admin/js/jquery.js"></script>
        <script type="text/javascript" src="__PUBLIC__/admin/js/bootstrap.min.js"></script>
        <script type="text/javascript" src="__PUBLIC__/admin/js/index.js"></script>
        <script type="text/javascript" src="__PUBLIC__/admin/js/time.js"></script>
        <script type="text/javascript" src="__PUBLIC__/admin/js/pages.js"></script>
        <style type="text/css">
            #lists{
                display: flex;
                justify-content: space-around;
                width: 100%;
            }
            #lists li{
                margin: 0 5px;
                text-align: left;
                width: 25%;
            }
        </style>
    </head>
<body>
    <div class="right-sidebar" style="max-width: 1200px;">
        <ul class="nav nav-tabs" style="margin-bottom: 16px" role="tablist">
            <li role="presentation" class="active"><a href="gameRecordList.html">{:L(GAME_RECORD_LIST)}</a></li>
            <li role="presentation"><a href="gameRecord.html">比赛记录查询</a></li>
        </ul>

        <section class="table-responsive">
            <table class="table table-bordered">
                <thead>
                <tr>
                    <th>比赛流水号</th>
                    <th>比赛时间</th>
                    <th>昵称</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody id="dataList"></tbody>
            </table>
        </section>
        <div id="playerPage" style="margin: 10px auto">
            <a href="javascript:void(0)" id="prev">上一页</a>
            <a href="javascript:void(0)" id="page"></a>
            <a href="javascript:void(0)" id="next">下一页</a>
            <span>一共</span>
            <a href="javascript:void(0)" id="totalPage"></a>
            <span>页</span>
        </div>
    </div>

    <script type="text/javascript">
        $.ajax({
            type:"get",
            url:"{:U('Admin/Main/getGameRecordList')}",
            success: function (data) {
               if(data == 'error') {
                   $("#dataList").html("暂时没查询到任何数据");
               }else{
                   if(data.arr == null || data.arr == '') {
                       $("#dataList").html("暂时没查询到任何数据");
                   }else{
                       var count = Math.ceil(data.count/10);
                       var str = '';
                       function  compare(a,b) {
                           var a1 = a.rank;
                           var b1 = b.rank;
                           if(a1 < b1) {
                               return -1;
                           }else if(a1 > b1) {
                               return 1;
                           }else{
                               return 0;
                           }
                       }
                       var arr = [];
                       for(var i=0;i<data.arr.length;++i) {
                            arr[i] = [];
                            for(var att in data.arr[i].users) {
                                arr[i].push(data.arr[i].users[att]);
                            }
                            arr[i].sort(compare);
                            if(data.arr[i].hasOwnProperty("mark") && data.arr[i].mark == 1) {
                                str += "<tr><td class='matchSN'>"+data.arr[i].matchSN+"</td><td>"+getLocalTime(data.arr[i].startTime)+"</td><td><ul id='lists'><li>"+arr[i][0].nick+"</li><li>"+arr[i][1].nick+"</li><li>"+arr[i][2].nick+"</li><li>"+arr[i][3].nick+"</li></ul></td><td><button class='btn btn-primary btn-sm' onclick='onClick(this)'>奖励发放</button></td></tr>";
                            }else{
                                str += "<tr><td class='matchSN'>"+data.arr[i].matchSN+"</td><td>"+getLocalTime(data.arr[i].startTime)+"</td><td><ul id='lists'><li>"+arr[i][0].nick+"</li><li>"+arr[i][1].nick+"</li><li>"+arr[i][2].nick+"</li><li>"+arr[i][3].nick+"</li></ul></td><td><button class='btn btn-success btn-sm' disabled onclick='onClick(this)'>已经发放</button></td></tr>";
                            }
                       }
                       $("#dataList").html(str);
                       $("#totalPage").text(count);
                       $("#page").text(1);
                   }
               }
            }
        });
        $("#prev").on("click",function (ev) {
            var page = $.trim($("#page").text());
            if(page == 1) {
                page = 1;
                return false;
            }else if($("#page").text() > 1){
                page = page -1;
            }
            var start = $("#dataList tr:first").find("td.matchSN").text();
            var action = -1;
            $.ajax({
                type:"post",
                url:"{:U('Admin/Main/getGameRecordList')}",
                dataType:"json",
                data:{"start":start,"action":action,"page":page},
                success: function (data) {
                    if(data == 'error') {
                        $("#dataList").html("暂时没查询到任何数据");
                    }else{
                        if(data.arr == null || data.arr == '') {
                            $("#dataList").html("暂时没查询到任何数据");
                        }else{
                            var count = Math.ceil(data.count/10);
                            var str = '';
                            function  compare(a,b) {
                                var a1 = a.rank;
                                var b1 = b.rank;
                                if(a1 < b1) {
                                    return -1;
                                }else if(a1 > b1) {
                                    return 1;
                                }else{
                                    return 0;
                                }
                            }
                            var arr = [];
                            for(var i=0;i<data.arr.length;++i) {
                                arr[i] = [];
                                for(var att in data.arr[i].users) {
                                    arr[i].push(data.arr[i].users[att]);
                                }
                                arr[i].sort(compare);
                                if(data.arr[i].hasOwnProperty("mark") && data.arr[i].mark == 1) {
                                    str += "<tr><td class='matchSN'>"+data.arr[i].matchSN+"</td><td>"+getLocalTime(data.arr[i].startTime)+"</td><td><ul id='lists'><li>"+arr[i][0].nick+"</li><li>"+arr[i][1].nick+"</li><li>"+arr[i][2].nick+"</li><li>"+arr[i][3].nick+"</li></ul></td><td><button class='btn btn-primary btn-sm' onclick='onClick(this)'>奖励发放</button></td></tr>";
                                }else{
                                    str += "<tr><td class='matchSN'>"+data.arr[i].matchSN+"</td><td>"+getLocalTime(data.arr[i].startTime)+"</td><td><ul id='lists'><li>"+arr[i][0].nick+"</li><li>"+arr[i][1].nick+"</li><li>"+arr[i][2].nick+"</li><li>"+arr[i][3].nick+"</li></ul></td><td><button class='btn btn-success btn-sm' disabled onclick='onClick(this)'>已经发放</button></td></tr>";
                                }
                            }
                            $("#dataList").html(str);
                            $("#page").text(page);
                        }
                    }
                }
            })
        });

        $("#next").on("click",function (e) {
            var page = $.trim($("#page").text());
            page = ++page;
            var totalPage = $.trim($("#totalPage").text())/1;
            if(page > totalPage) {
                return false;
            }
            var start = $("#dataList tr:last").find("td.matchSN").text();
            var action = 1;
            $.ajax({
                type:"post",
                url:"{:U('Admin/Main/getGameRecordList')}",
                dataType:"json",
                data:{"start":start,"action":action,"page":page},
                success: function (data) {
                    if(data == 'error') {
                        $("#dataList").html("暂时没查询到任何数据");
                    }else{
                        if(data.arr == null || data.arr == '') {
                            $("#dataList").html("暂时没查询到任何数据");
                        }else{
                            var count = Math.ceil(data.count/10);
                            var str = '';
                            function  compare(a,b) {
                                var a1 = a.rank;
                                var b1 = b.rank;
                                if(a1 < b1) {
                                    return -1;
                                }else if(a1 > b1) {
                                    return 1;
                                }else{
                                    return 0;
                                }
                            }
                            var arr = [];
                            for(var i=0;i<data.arr.length;++i) {
                                arr[i] = [];
                                for(var att in data.arr[i].users) {
                                    arr[i].push(data.arr[i].users[att]);
                                }
                                arr[i].sort(compare);
                                if(data.arr[i].hasOwnProperty("mark") && data.arr[i].mark == 1) {
                                    str += "<tr><td class='matchSN'>"+data.arr[i].matchSN+"</td><td>"+getLocalTime(data.arr[i].startTime)+"</td><td><ul id='lists'><li>"+arr[i][0].nick+"</li><li>"+arr[i][1].nick+"</li><li>"+arr[i][2].nick+"</li><li>"+arr[i][3].nick+"</li></ul></td><td><button class='btn btn-primary btn-sm' onclick='onClick(this)'>奖励发放</button></td></tr>";
                                }else{
                                    str += "<tr><td class='matchSN'>"+data.arr[i].matchSN+"</td><td>"+getLocalTime(data.arr[i].startTime)+"</td><td><ul id='lists'><li>"+arr[i][0].nick+"</li><li>"+arr[i][1].nick+"</li><li>"+arr[i][2].nick+"</li><li>"+arr[i][3].nick+"</li></ul></td><td><button class='btn btn-success btn-sm' disabled onclick='onClick(this)'>已经发放</button></td></tr>";
                                }
                            }
                            $("#dataList").html(str);
                            $("#page").text(page);
                        }
                    }
                }
            });
        });

        function onClick(This) {
            var matchSN = $(This).closest("tr").find("td.matchSN").text();
            var txt = $(This).text();
            var mark = 0;
            if(txt === "奖励发放") {
                mark = 0;
                $.ajax({
                    type:"get",
                    url:"{:U('Admin/Main/setGameRecordMark')}",
                    dataType:"json",
                    data:{"matchSN":matchSN,"mark":mark},
                    success: function (data) {
                        console.log(data);
                        if(data == 'success') {
                            alert("发放成功");
                            window.location.href="{:U('Admin/Main/gameRecordList')}";
                        }
                    }
                })
            }else{
                alert("该奖励已发放");
            }
        }
    </script>

</body>
</html>